package top.aoyudi.service.serviceImpl;


import org.apache.poi.ss.usermodel.*;
import top.aoyudi.service.IExcelService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelServiceImpl implements IExcelService {
    public List<String> getExcelFields(Sheet sheet, int line) {
        List<String> fieldList = new ArrayList<>();
        // 遍历工作表属性行
        for (Cell cell : sheet.getRow(line - 1)) {
            fieldList.add("`" + cell+"`");
        }
        return fieldList;
    }

    public Map<Integer, List<String>> getExcelData(Sheet sheet, int firstLine, int lastLine) {
        // 工作表全部数据
        int map=0;
        Map<Integer, List<String>> dataMap = new HashMap<>();
        for (int i = firstLine; i <= lastLine; i++) {
            Row row = sheet.getRow(i-1);
            List<String> roleDataList = new ArrayList<>();
            for (Cell cell : row) {
                // 设置为String格式
                cell.setCellType(Cell.CELL_TYPE_STRING);
                roleDataList.add(cell.getStringCellValue());
            }
            dataMap.put(map++, roleDataList);
        }
        return dataMap;
    }

    public Map<Integer, List<String>> getExcelStringData(Sheet sheet, int firstLine, int lastLine) {
        // 工作表全部数据
        int map=0;
        Map<Integer, List<String>> dataMap = new HashMap<>();
        for (int i = firstLine; i <= lastLine; i++) {
            Row row = sheet.getRow(i-1);
            List<String> rowDataList = new ArrayList<>();
            for (Cell cell : row) {
                // 设置为String格式
                cell.setCellType(Cell.CELL_TYPE_STRING);
                rowDataList.add("\""+cell.getStringCellValue()+"\"");
            }
            dataMap.put(map++, rowDataList);
        }
        return dataMap;
    }
}
